// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ExtendedPropertyCollectionExtensions.cs" company="Akzente.IT">
//   (c) 2012 Akzente.IT
// </copyright>
// <summary>
//   Extends the ExtendedPropeertyCollection of SMO objects to make is easier to get and set the "MS_Description" property
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace AkzenteIT.DatabaseDocumentationTool.Extensions
{
    using Microsoft.SqlServer.Management.Smo;

    /// <summary>
    /// Extends the ExtendedPropeertyCollection of SMO objects to make is easier to get and set the "MS_Description" property
    /// </summary>
    public static class ExtendedPropertyCollectionExtensions
    {
        #region Constants

        /// <summary>
        /// The name.
        /// </summary>
        private const string Name = "MS_Description";

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The get description.
        /// </summary>
        /// <param name="collection">
        /// The collection.
        /// </param>
        /// <returns>
        /// The System.String.
        /// </returns>
        public static string GetDescription(this ExtendedPropertyCollection collection)
        {
            if (collection.Contains(Name))
            {
                var val = collection[Name].Value;
                return val == null ? null : val.ToString();
            }

            return null;
        }

        /// <summary>
        /// The set description.
        /// </summary>
        /// <param name="collection">
        /// The collection.
        /// </param>
        /// <param name="value">
        /// The value.
        /// </param>
        /// <returns>
        /// The System.String.
        /// </returns>
        public static string SetDescription(this ExtendedPropertyCollection collection, string value)
        {
            if (collection.Contains(Name))
            {
                collection[Name].Value = value;
            }
            else
            {
                collection.Add(new ExtendedProperty(collection.Parent, Name, value));
            }

            return null;
        }

        #endregion
    }
}